package com.easyexcle.demo.controller.easypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.easyexcle.demo.easypoi.domain.ExcelExportVo;
import com.easyexcle.demo.easypoi.domain.ExportVo;
import com.easyexcle.demo.easypoi.util.ExcelStyleUtil;
import com.easyexcle.demo.easypoi.util.ExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lkz
 * @Title: EasyPoiRest
 * @Description: easyPoi测试
 * @Date: 2022/12/27 14:50
 */
@RequestMapping("easyPoi")
@RestController
public class EasyPoiRest {

    /**
     * 多级表头、多个sheet导出
     */
    @RequestMapping("/export")
    public void export(HttpServletResponse response){
        // 模拟数据
        List<ExcelExportVo> dataList = new ArrayList<>();
        List<ExcelExportVo.UserInfo> userInfoList = new ArrayList<>();
        List<ExcelExportVo.RoleInfo> roleInfoList = new ArrayList<>();
        userInfoList.add(new ExcelExportVo.UserInfo("chenyi","陈仪","12345678910","1","战神联盟","科研部"));
        userInfoList.add(new ExcelExportVo.UserInfo("honger","洪洱","13241220000","2","前途有限公司","市场部"));
        userInfoList.add(new ExcelExportVo.UserInfo("zhangsan","张三","12544445555","0","前途有限公司","研发部"));
        userInfoList.add(new ExcelExportVo.UserInfo("lisi","李四","13125223561","1","战神联盟","科研部"));
        userInfoList.add(new ExcelExportVo.UserInfo("wangwu","王五","15423226355","2","战神联盟","销售部"));

        roleInfoList.add(new ExcelExportVo.RoleInfo("经理","JL","sys,dept,role,menu","view,add,update,delete"));
        roleInfoList.add(new ExcelExportVo.RoleInfo("经理","JL","","view,add,update,delete"));
        roleInfoList.add(new ExcelExportVo.RoleInfo("组长","ZZ","role,menu","view,add,update"));
        roleInfoList.add(new ExcelExportVo.RoleInfo("普通","PT","menu","view,add"));
        roleInfoList.add(new ExcelExportVo.RoleInfo("普通","PT","menu","view,add"));

        ExcelExportVo vo = new ExcelExportVo();
        vo.setUserInfoList(userInfoList);
        vo.setRoleInfoList(roleInfoList);
        dataList.add(vo);

        // 第一个sheet
        ExportParams params1 = new ExportParams();
        params1.setStyle(ExcelStyleUtil.class);
        params1.setSheetName("用户信息1");
        params1.setTitle("用户信息");
        Map<String, Object> params1Map = new HashMap<>();
        params1Map.put("title", params1);
        params1Map.put("entity", ExcelExportVo.class);
        params1Map.put("data", dataList);

        // 第二个sheet
        ExportParams params2 = new ExportParams();
        params2.setStyle(ExcelStyleUtil.class);
        params2.setSheetName("用户信息2");
        Map<String, Object> params2Map = new HashMap<>();
        params2Map.put("title", params2);
        params2Map.put("entity", ExcelExportVo.class);
        params2Map.put("data", new ArrayList<>());

        // 第三个sheet
        ExportParams params3 = new ExportParams();
        params3.setStyle(ExcelStyleUtil.class);
        params3.setSheetName("用户信息3");
        Map<String, Object> params3Map = new HashMap<>();
        params3Map.put("title", params3);
        params3Map.put("entity", ExcelExportVo.class);
        params3Map.put("data", new ArrayList<>());

        // 将3个sheet封装
        List<Map<String, Object>> sheetsList = new ArrayList<>();
        sheetsList.add(params1Map);
        sheetsList.add(params2Map);
        sheetsList.add(params3Map);
        Workbook workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
        ExcelUtil.downLoadExcel("用户信息",response,workBook);
    }
    @GetMapping("temp")
    public void testTemp(HttpServletResponse response){
        Workbook sheets = ExcelExportUtil.exportExcel(new ExportParams(), ExportVo.class, new ArrayList<>());
        ExcelUtil.downLoadExcel("temp",response,sheets);
    }


}
